package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import security.Authority;
import security.UserAccount;
import services.ParticipantService;
import services.UserAccountService;
import domain.Participant;
import forms.ParticipantForm;

@Controller
@RequestMapping("/account/participant")
public class ParticipantAccountController extends AbstractController{
	
	// Constructors -----------------------------------------------------------

		public ParticipantAccountController() {
			super();
		}

	// Services ---------------------------------------------------------------
		
		@Autowired
		private UserAccountService userAccountService;
		
	
		@Autowired
		private ParticipantService ParticipantService;
				
		
		@RequestMapping(value = "/create", method = RequestMethod.GET)
		public ModelAndView editParticipant() {
			ModelAndView result;
			UserAccount u = userAccountService.create();
			Authority a = new Authority();
			a.setAuthority("PARTICIPANT");
			u.addAuthority(a);
			Participant e = ParticipantService.create();
			e.setUserAccount(u);
			result = createEditModelAndView(ParticipantService.getFormObject(e));

			return result;
		}
		
		
		
		protected ModelAndView createEditModelAndView(ParticipantForm e) {
			ModelAndView result;

			result = createEditModelAndView(e, null);
			
			return result;
		}	
		
		protected ModelAndView createEditModelAndView(ParticipantForm e, String message) {
			ModelAndView result;;
			result = new ModelAndView("account/new");
			result.addObject("participantForm",e);
			result.addObject("requestURI", "account/participant/edit.do");
			result.addObject("type","participantForm");
			result.addObject("isParticipant", true);
			result.addObject("message",message);
			return result;
		}
		
//		@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
//		public ModelAndView saveParticipant(@Valid Participant Participant,
//				BindingResult binding) {
//			ModelAndView result;
//			
//			if (binding.hasErrors()) {
//				result = createEditModelAndView(Participant);
//
//			} else {
//				try {
//					
//					ParticipantService.save(Participant);
//					SimpleDateFormat formatter;
//					String moment;
//					result = new ModelAndView("welcome/index");
//					formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
//					moment = formatter.format(new Date());
//					result.addObject("moment", moment);
//					result.addObject("message", "account.commit.ok");
//		
//				} catch (Throwable oops) {
//					result = createEditModelAndView(Participant,
//							"account.commit.error");
//
//				}
//
//			}
//
//			return result;
//		}
		
		@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
		public ModelAndView saveParticipant(@Valid ParticipantForm participantForm,
				BindingResult binding) {
			ModelAndView result;
			
			if(!participantForm.getUserAccount().getPassword().equals(participantForm.getRepeatedPassword())){
				result = createEditModelAndView(participantForm, "account.passwords.notmatch");
			}
			else if(!participantForm.getHasAccepted()){
				result = createEditModelAndView(participantForm, "account.legal.checked");
			}
			else{
				
				if (binding.hasErrors()) {
					result = createEditModelAndView(participantForm);
					}
				else{
				try {
					Participant participant = ParticipantService.reconstruct(participantForm);
					ParticipantService.save(participant);
					SimpleDateFormat formatter;
					String moment;
					result = new ModelAndView("welcome/index");
					formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
					moment = formatter.format(new Date());
					result.addObject("moment", moment);
					result.addObject("message", "account.commit.ok");
		
				} catch (Throwable oops) {
					result = createEditModelAndView(participantForm,
							"account.commit.error");
				}
			}
			}

			return result;
		}

		
}
